package project.taobao.commons.activemq.send.imple;

import org.apache.activemq.ActiveMQConnectionFactory;
import project.taobao.commons.activemq.utils.ActiveMqUtils;
import project.taobao.commons.activemq.send.Producer;

import javax.jms.*;

public class JMSProducer implements Producer {
    // methods
    public void sendMessage(String queueName, Object message){
        ActiveMQConnectionFactory connectionFactory = ActiveMqUtils.getAcMQConnectionFactory();
        Connection connection = null;
        Session session = null;
        try{
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            Queue destination = session.createQueue(queueName);
            MessageProducer messageProducer = session.createProducer(destination);
            MapMessage mapMessage = session.createMapMessage();
            mapMessage.setObject("data", message);
            messageProducer.send(mapMessage);
            session.commit();
        } catch (Exception e){

        } finally {
            try{
                if(connection != null){
                    connection.close();
                }
            } catch (Exception e){

            }
        }
    }

    @Override
    public void sendMessage(Destination queue, Object message) {

    }
    // getters, setters

    // constructors
}
